/**
 * @ProjectName: p2p_core_sina
 * @Copyright: Copyright (c) 2016, qudoulicai All Rights Reserved. 
 * @address: Hangzhou City, Zhejiang Province, China
 * @date: 2016年1月18日 下午2:17:35
 * @Description: 微信菜单
 */
package com.qdlc.p2p.biz.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.WeChatMenuService;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.WeChatMenuDao;
import com.qdlc.p2p.dal.dto.WeChatMenu;
import com.qdlc.p2p.dal.model.WeChatMenuModel;

/**
 * <p>微信菜单service接口实现类</p>
 *
 * @author Kaiju Zhao 2016年1月18日 下午2:17:35
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2016年1月18日 下午2:17:35
 * @modify by reason:{原因}
 */
@Service("weChatMenuService")
public class WeChatMenuServiceImpl implements WeChatMenuService {

    @Resource
    private WeChatMenuDao weChatMenuDao;

    @Override
    public WeChatMenu addWeChatMenu(WeChatMenu weChatMenu) {
        if (null == weChatMenu) {
            throw new BusinessException("添加的参数weChatMenu为null");
        }

        long id = weChatMenu.getId();
        if (0L < id) {
            throw new BusinessException("添加的参数weChatMenu的id大于0");
        }

        int sort = 1;
        List<WeChatMenu> weChatMenuList = this.weChatMenuDao.findAll();
        if (null != weChatMenuList) {
            sort = weChatMenuList.size() + 1;
        }
        weChatMenu.setAddTime(DateUtil.getNow());
        weChatMenu.setUpdateTime(DateUtil.getNow());
        weChatMenu.setSort(sort);
        return this.weChatMenuDao.save(weChatMenu);
    }

    @Override
    public WeChatMenu updateWeChatMenu(WeChatMenu weChatMenu) {
        if (null == weChatMenu) {
            throw new BusinessException("修改的参数weChatMenu为null");
        }

        WeChatMenu oldWeChatMenu = this.weChatMenuDao.find(weChatMenu.getId());
        if (null == oldWeChatMenu) {
            throw new BusinessException("修改的参数weChatMenu在数据库中不存在");
        }

        weChatMenu.setUpdateTime(DateUtil.getNow());
        return this.weChatMenuDao.update(weChatMenu);
    }

    @Override
    public WeChatMenu findWeChatMenuById(long id) {
        WeChatMenu weChatMenu = this.weChatMenuDao.find(id);
        if (null != weChatMenu
                && weChatMenu.getIsDelete() == true) { //如果已被删除，将其设置为null
            weChatMenu = null;
        }
        return weChatMenu;
    }

    @Override
    public void deleteWeChatMenu(WeChatMenu weChatMenu) {
        if (null == weChatMenu) {
            throw new BusinessException("删除的参数weChatMenu为null");
        }

        WeChatMenu oldWeChatMenu = this.weChatMenuDao.find(weChatMenu.getId());
        if (null == oldWeChatMenu) {
            throw new BusinessException("删除的参数weChatMenu在数据库中不存在");
        }

        this.weChatMenuDao.deleteById(weChatMenu.getId());
    }

    @Override
    public List<WeChatMenu> listByIsDelete(boolean isDelete) {
        return this.weChatMenuDao.listByIsDelete(isDelete);
    }

    @Override
    public void deleteByParentId(long parentId) {
        this.weChatMenuDao.deleteByParentId(parentId);
    }

    @Override
    public List<WeChatMenuModel> listByParentIdAndIsDelete(long parentId, boolean isDelete) {
        List<WeChatMenuModel> modelList = new ArrayList<WeChatMenuModel>();

        List<WeChatMenu> list = this.weChatMenuDao.listByParentIdAndIsDelete(parentId, isDelete);
        if (null != list
                && list.size() > 0) {
            for (WeChatMenu weChatMenu : list) {
                modelList.add(WeChatMenuModel.instance(weChatMenu));
            }
        }
        return modelList;
    }

    @Override
    public List<WeChatMenuModel> listModelsByIsDelete(boolean isDelete) {
        List<WeChatMenuModel> modelList = new ArrayList<WeChatMenuModel>();

        List<WeChatMenu> list = this.listByIsDelete(isDelete);
        if (null != list
                && list.size() > 0) {
            for (WeChatMenu weChatMenu : list) {
                modelList.add(WeChatMenuModel.instance(weChatMenu));
            }
        }
        return modelList;
    }

    @Override
    public List<WeChatMenuModel> listByLevelAndIsDelete(byte level, boolean isDelete) {
        List<WeChatMenuModel> modelList = new ArrayList<WeChatMenuModel>();

        List<WeChatMenu> list = this.weChatMenuDao.listByLevelAndIsDelete(level, isDelete);
        if (null != list
                && list.size() > 0) {
            for (WeChatMenu weChatMenu : list) {
                modelList.add(WeChatMenuModel.instance(weChatMenu));
            }
        }
        return modelList;
    }
}
